package pa165.dominatingspecies.frontend.environment;

import java.util.ArrayList;
import java.util.List;
import org.apache.wicket.markup.html.basic.Label;
import org.apache.wicket.markup.html.form.Button;
import org.apache.wicket.markup.html.form.Form;
import org.apache.wicket.markup.html.form.TextArea;
import org.apache.wicket.markup.html.form.TextField;
import org.apache.wicket.model.CompoundPropertyModel;
import org.apache.wicket.model.PropertyModel;
import org.apache.wicket.model.StringResourceModel;
import org.apache.wicket.spring.injection.annot.SpringBean;
import org.springframework.dao.DataAccessException;
import org.springframework.security.access.prepost.PreAuthorize;
import pa165.dominating.species.enums.ElementType;
import pa165.dominating.species.service.ElementService;
import pa165.dominating.species.tos.ElementTo;
import pa165.dominating.species.tos.EnvironmentTo;
import pa165.dominatingspecies.frontend.element.EnumListMultiple;
import pa165.dominatingspecies.frontend.pages.BasePage;
import pa165.dominatingspecies.service.impl.EnvironmentServiceImpl;

/**
 *
 * @author Stanislau Dubrouski
 */
@PreAuthorize("hasRole('WORKER')")
public class EditEnvironmentPage extends BasePage {

    @SpringBean(name = "environmentService")
    private EnvironmentServiceImpl envService;
    @SpringBean(name = "elementService")
    private ElementService elementService;
    private EnvironmentTo env;
    private List<ElementType> selected = new ArrayList();

    EditEnvironmentPage(Long id) {
        if (id != null) {
            env = envService.getEnvironmentById(id);

        }

        Form<EnvironmentTo> form = new Form<>("createEnvironmentForm");
        form.setModel(new CompoundPropertyModel<>(env));
        form.add(new Label("nameLabel", new StringResourceModel("envNameLabel", this, null)));
        form.add(new TextField<String>("name"));
        form.add(new Label("desc", new StringResourceModel("envDescLabel", this, null)));
        form.add(new TextArea<String>("description"));
        form.add(new Label("eleType", new StringResourceModel("selectElements", this, null)));
        form.add(new BackToListLink("backToList"));

        List<ElementTo> allOwn = env.getElements();
        for (ElementTo to : allOwn) {
            selected.add(to.getType());
        }
        form.add(new EnumListMultiple("elements", new PropertyModel(this, "selected")));

        Button submitButton = new Button("submitButton") {
            @Override
            public void onSubmit() {
                boolean errorsFound = false;
                if (env.getName() == null) {
                    getSession().warn(new StringResourceModel("envNameEmptyWarn", this, null).getString());
                    errorsFound = true;
                }
                if (env.getDescription() == null) {
                    getSession().warn(new StringResourceModel("envDescEmptyWarn", this, null).getString());
                    errorsFound = true;
                }
                if (selected.isEmpty()) {
                    getSession().warn(new StringResourceModel("envNoElemsSelectedWarn", this, null).getString());
                    errorsFound = true;
                }

                if (!errorsFound) {
                    boolean thrown = false;
                    try {
                        List<ElementTo> found = new ArrayList<>();
                        for (ElementType t : selected) {
                            found.add(elementService.getElementToByType(t));
                        }
                        env.setElements(found);
                        envService.updateEnvironment(env);
                    } catch (DataAccessException ex) {
                        getSession().warn(new StringResourceModel("envUpdateFail", this, null, new Object[]{env.getId()}).getString());
                        thrown = true;
                    }
                    if (!thrown) {
                        getSession().info(new StringResourceModel("envUpdateSucc", this, null, new Object[]{env.getId()}).getString());
                        setResponsePage(EnvironmentsPage.class);
                    }
                }
            }
        };
        form.add(submitButton);
        add(form);
    }
}
